package com.example.buildtable.controller;

import com.example.common.util.R;
import com.example.buildtable.pojo.System;
import com.example.buildtable.service.SystemService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@Tag(name = "系统")
@RequestMapping(value = "/system/")
public class SystemController {
    @Autowired
    private SystemService systemService;
    
    @Operation(summary = "查询所有系统数据")
    @GetMapping(value = "/getAllSystem")
    public R getAllSystem() {
        List<System> systemList = systemService.list();
        return R.ok("查询所有系统数据成功").data("systemList", systemList);
    }
    
    @Operation(summary = "增加系统")
    @PostMapping(value = "/addSystem")
    public R addSystem(System system) {
        boolean flag = systemService.save(system);
        if (flag) {
            return R.ok("增加系统成功");
        } else {
            return R.ok("增加系统失败");
        }
    }

    @Operation(summary = "删除系统")
    @GetMapping(value = "/deleteSystem/{systemId}")
    public R deleteSystem(@PathVariable("systemId") Integer systemId) {
        boolean flag = systemService.removeById(systemId);
        if (flag) {
            return R.ok("删除系统成功");
        } else {
            return R.ok("删除系统失败");
        }
    }

    @Operation(summary = "按照系统查看")
    @GetMapping(value = "/getSystemById/{systemId}")
    public R getSystemById(@PathVariable("systemId") Integer systemId) {
        System system = systemService.getById(systemId);
        if (system != null) {
            return R.ok("按照系统查看成功").data("system", system);
        } else {
            return R.error("按照系统查看失败");
        }
    }

    @Operation(summary = "修改系统")
    @PostMapping(value = "/updateSystem")
    public R updateSystem(System system) {
        boolean flag = systemService.updateById(system);
        if (flag) {
            return R.ok("修改系统成功");
        } else {
            return R.ok("修改系统失败");
        }
    }
}